package com.shopping.mall.base.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.InputStream;

public class FileUtils {
    /**
     * 判断是否为图片
     */
    public static Boolean isImage(InputStream inputStream) {
        try {
            if (inputStream == null) {
                return false;
            }
            Image img = ImageIO.read(inputStream);
            return !(img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 通过文件名获得文件后缀
     */
    public static String getFileSuffix(String fileName) {
        if (fileName == null) {
            return null;
        }
        String[] name = fileName.split("\\.");
        if (name.length == 1) {
            return null;
        }
        return "." + name[name.length - 1];
    }

    /**
     * 判断文件是不是excel
     */
    public static Boolean isExcel(String fileName) {
        String suffix = getFileSuffix(fileName);
        return suffix.equals(".xls") || suffix.equals(".xlsx");
    }
}
